package com.example.common.vm

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.common.intent.BaseViewIntent
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch

//ViewModel基类
abstract class BaseViewModel<I:BaseViewIntent>:ViewModel(){

    var channel:Channel<I> = Channel(Channel.UNLIMITED)
    init {
        viewModelScope.launch {
            //接受view层Intent
            channel.consumeAsFlow().collect{
                dispatchIntent(it)
            }
        }
    }
    //分发意图
    abstract fun dispatchIntent(intent: I)


}